﻿#region Namespaces
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using WebReference;
using System.Data.SqlClient;
#endregion

public partial class Admin_AshAnalysis : System.Web.UI.Page
{
    Service objService = new Service();

    #region UI Events

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
        }
        else
        {
            string ExperimentId = (string)Session["ExperimentId"];
            if (ExperimentId != null)
            {
                lblExpdetails.Text = "Your Experiment Id is - " + ExperimentId;
            }
            string id = Request.QueryString["id"];

            if (id != null)
            {
                fillAshanalysisDetails(Convert.ToInt32(id));
                txtAshAnalId.Text = string.Empty;
                trAshAnalysisDataGrid.Visible = false;
                trAshAnalTextbox.Visible = true;
            }

            //trAshAnalysisDataGrid.Visible = true;
            bindDataGrid();
        }
    }
    protected void dgAshAnalysis_RowEditing(object sender, GridViewEditEventArgs e)
    {
        int id = Convert.ToInt32(dgAshAnalysis.Rows[e.NewEditIndex].Cells[1].Text.Trim());
        fillAshanalysisDetails(id);
        trAshAnalysisDataGrid.Visible = false;
        trAshAnalTextbox.Visible = true;
        btnPreviousAshAnalysis.Visible = false;

    }

    protected void dgAshAnalysis_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        dgAshAnalysis.PageIndex = e.NewPageIndex;
        bindDataGrid();
    }

    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        string RefeId = (string)Session["ReferenceId"];

        if (txtAshAnalId.Text != string.Empty)
        {
            lblMessage.Text = objService.UpdateAshAnalysis(txtAshProductionMethod.Text, txtCO2.Text, txtSO3.Text, txtP2O5.Text, txtSiO2.Text, txtFe2O3.Text, txtAl2O3.Text, txtCaO.Text, txtMgO.Text, txtNa2O.Text, txtK2O.Text, txtTiO2.Text, txtPb.Text, txtCd.Text, txtCl.Text, txtCu.Text, txtHg.Text, txtMn.Text, txtCr.Text, Convert.ToInt32(txtAshAnalId.Text));
            bindDataGrid();
            clearTextboxes();
        }

        else
        {
            lblMessage.Text = objService.SaveAshAnalysis(txtAshProductionMethod.Text, txtCO2.Text, txtSO3.Text, txtP2O5.Text, txtSiO2.Text, txtFe2O3.Text, txtAl2O3.Text, txtCaO.Text, txtMgO.Text, txtNa2O.Text, txtK2O.Text, txtTiO2.Text, txtPb.Text, txtCd.Text, txtCl.Text, txtCu.Text, txtHg.Text, txtMn.Text, txtCr.Text);

            string AshAnalysisId = objService.GetLastInsertedId("ref_fk", RefeId, "ashanal");
               
            objService.UpdateTable("ashanal", "ref_fk", RefeId, AshAnalysisId);

            clearTextboxes();

            if ((string)Session["ExperimentId"] != null)
            {
                string ExpId = (string)Session["ExperimentId"];
                if (objService.UpdateExperiment("ashanal_fk", AshAnalysisId, ExpId) != "Experiment Successfully Updated")
                {
                    objService.DeleteRecord("ashanal", Convert.ToInt32(AshAnalysisId));
                    lblMessage.Text = "Admin - You can't enter the multiple Analysis record for one experiment";
                    bindDataGrid();
                }
                else
                {
                    lblMessage.Text = "Ash Analysis Saved and " + objService.UpdateExperiment("ashanal_fk", AshAnalysisId, ExpId);
                    bindDataGrid();
                }

                lblExpdetails.Text = "Your Experiment Id is - " + ExpId;
            }
            else
            {

                lblMessage.Text = "Ash Analysis Saved and " + objService.SaveExperiment(null, AshAnalysisId, null, null, null, null, null, null, null, null, null, RefeId, null);
                string ExperimentId = objService.GetExperimentId("ashanal_fk", AshAnalysisId);
                Session["ExperimentId"] = ExperimentId;
                lblExpdetails.Text = "Your Experiment Id is -" + ExperimentId;
                bindDataGrid();

            }
        }
    }

    #endregion

    #region Private methods

    private void bindDataGrid()
    {
        string ExpId = (string)Session["ExperimentId"];

        if (ExpId != null)
        {
            Int32 AshAnalysisId = objService.GetExperimentFKValues("ashanal_fk", ExpId);

            if (AshAnalysisId.ToString() == null)
            {
                lblMessage.Text = "You haven't entered any data for the Ash analysis to this experiment";
            }
            else
            {

                if (objService.GetAshAnalysis(AshAnalysisId.ToString()).Tables[0].Rows.Count < 1)
                {
                    trAshAnalTextbox.Visible = true;
                    trAshAnalysisDataGrid.Visible = false;
                }
                else
                {
                    trAshAnalTextbox.Visible = false;
                    dgAshAnalysis.DataSource = objService.GetAshAnalysis(AshAnalysisId.ToString());
                    dgAshAnalysis.DataBind();
                    trAshAnalysisDataGrid.Visible = true;
                }
                
            }
        }
        else
        {
            lblMessage.Text = "You haven't entered any data for the Ash analysis to this experiment";
        }
    }

    private void clearTextboxes()
    {
        txtAshProductionMethod.Text=string.Empty ;
        txtCO2.Text=string.Empty ;
        txtSO3.Text=string.Empty ;
        txtP2O5.Text=string.Empty ;
        txtSiO2.Text=string.Empty ;
        txtFe2O3.Text=string.Empty ;
        txtAl2O3.Text=string.Empty ;
        txtCaO.Text=string.Empty ;
        txtMgO.Text=string.Empty ;
        txtNa2O.Text=string.Empty ;
        txtK2O.Text=string.Empty ;
        txtTiO2.Text=string.Empty ;
        txtPb.Text=string.Empty ;
        txtCd.Text=string.Empty ;
        txtCl.Text=string.Empty ;
        txtCu.Text=string.Empty ;
        txtHg.Text=string.Empty ;
        txtMn.Text=string.Empty ;
        txtCr.Text=string.Empty ;
    }
            
    private void fillAshanalysisDetails(Int32  Id)
    {
    
        DataSet ds = new DataSet();
        ds = objService.GetAshanalysisDetails(Id);

        txtAshProductionMethod.Text = ds.Tables[0].Rows[0]["ashProdMethod"].ToString();
        txtCO2.Text = ds.Tables[0].Rows[0]["CO2"].ToString();
        txtSO3.Text = ds.Tables[0].Rows[0]["SO3"].ToString();
        txtP2O5.Text = ds.Tables[0].Rows[0]["P2O5"].ToString();
        txtSiO2.Text = ds.Tables[0].Rows[0]["SiO2"].ToString();
        txtFe2O3.Text = ds.Tables[0].Rows[0]["Fe2O3"].ToString();
        txtAl2O3.Text = ds.Tables[0].Rows[0]["Al2O3"].ToString();
        txtCaO.Text = ds.Tables[0].Rows[0]["CaO"].ToString();
        txtMgO.Text = ds.Tables[0].Rows[0]["MgO"].ToString();
        txtNa2O.Text = ds.Tables[0].Rows[0]["Na2O"].ToString();
        txtK2O.Text = ds.Tables[0].Rows[0]["K2O"].ToString();
        txtTiO2.Text = ds.Tables[0].Rows[0]["TiO2"].ToString();
        txtPb.Text = ds.Tables[0].Rows[0]["Pb"].ToString();
        txtCd.Text = ds.Tables[0].Rows[0]["Cd"].ToString();
        txtCl.Text = ds.Tables[0].Rows[0]["Cl"].ToString();
        txtCu.Text = ds.Tables[0].Rows[0]["Cu"].ToString();
        txtHg.Text = ds.Tables[0].Rows[0]["Hg"].ToString();
        txtMn.Text = ds.Tables[0].Rows[0]["Mn"].ToString();
        txtCr.Text = ds.Tables[0].Rows[0]["Cr"].ToString();
        txtAshAnalId.Text = ds.Tables[0].Rows[0]["id"].ToString();
    }

    #endregion


    protected void btnPreviousAshAnalysis_Click(object sender, EventArgs e)
    {
        Response.Redirect("Previous Pages/PreAshAnalysis.aspx");
    }
}